<?php
include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
include_once(dirname(__FILE__) . "/libs/nms_html_scripts.php");
include_once(dirname(__FILE__) . "/libs/nms_html_views.php");
include_once(dirname(__FILE__) . "/libs/nms_getdeviceinfor.php");
function draw_dim(){
	html_master_script_filter();
	html_device_script();
	html_device_script_popup();
	draw_dim_header();
	draw_dim_table();
	if (isset($_REQUEST['search'])) {
		?>
	    				<script >
	    					var tmp = $('#id_dim_table').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
	    					alert(test);
	    					$('#id_table_dim').empty();
	    					$('#id_table_dim').html(test);
	    				</script>
	    				<script >
	    					var tmp = $('#id_dim_table_1').removeHighlight().highlight('<?php echo trim($_GET['search']) ?>');
	    					alert(test);
	    					$('#id_table_dim_1').empty();
	    					$('#id_table_dim_1').html(test);
	    				</script>
	   	<?php 
	    }
}
function draw_dim_header(){
	?>
	<div id="id_master_center">
		<div id="id_master_header">
			<h2> Device Management </h2>
		</div>	
		<div id="id_master_center_top">
			<div id="id_master_center_top1">
			<table width="100%" border="0">
    			<tr>
	    		<!-- Status filter -->
	    			<!-- Devices filter -->
	    			<td colspan="1" scope="col" >Device:</td>
	    			<td>&nbsp;</td>
	    			<td  colspan="1" scope="col">
						<select  onchange="val()" id="id_host_dim">
							<option value=""> </option>
		    				<?php
	     					 $hosts = db_fetch_assoc("select id,CONCAT_WS('',description,' (',hostname,')') as name from host where description !='Localhost' order by description,hostname");      $result3 = db_fetch_assoc($sql54);
	     					 if (sizeof($hosts) > 0) {
						     foreach($hosts as $set) {
						     echo "<option value='" . $set["id"] . "'";
						     if(isset($_REQUEST["host_id"])){if($_REQUEST["host_id"] == $set["id"]){echo "selected";}};
						     echo ">" . htmlspecialchars($set["name"]) . "</option>\n";
						     }
						     }
	   						?>
		        		</select>
		        		<script type="text/javascript">
						$("#id_host_dim").chosen({
							search_contains: true,
							no_results_text:"Oops, nothing found!"});
					
 					 </script>
		    		</td>
		    		<td>&nbsp;</td>
		    		<?php 
		    		if(empty($_REQUEST['date'])){
						$_SESSION["current_date_dim"] = date("Y-m-d 00:00");
					}else{
						$_SESSION["current_date_dim"] = $_REQUEST['date'];
					}
					// request button refresh
					if(isset($_REQUEST['refreshid'])){
						$id = db_fetch_row('Select description,hostname,snmp_community From host Where id='.$_REQUEST['refreshid'].'');
						getdevice($_REQUEST['refreshid'], $id['hostname'], $id['snmp_community']);
						echo'<script>
								alert("Refresh host: '.$id['description'].' successfull!");
							</script>';
					}
					?>
	  				<!-- Button Calendar -->
	  				<td nowrap style='white-space: nowrap;'>
						<input type='text' name='date' id='date' title='Graph Begin Timestamp' size='15' value='<?php print (isset($_SESSION["current_date_dim"]) ? $_SESSION["current_date_dim"] : "");?>'>
					</td>
					<td nowrap style='white-space: nowrap;'>
						&nbsp;<input type='image' src='images/iconCalendar.gif' align='middle' alt='Start date selector' title='Start date selector' onclick="return showCalendar('date');">
					</td>
	  				<td>&nbsp;</td>
	  				<!-- Search -->
					<td colspan="3" scope="col"> Search:
	    				<input id="id_search" type="search" onkeypress="keydown(event)"/>
	    				<!-- Button Go -->
	    				<input id="btnGo" type="button" value="Go";> 
	    			</td>
	    			<td>
	    				<!-- Button Clear -->        
	    				<input id="btnClear" type="submit" name="clear_x" value="Clear" title="Clear Filters">
	    			</td>
	    			<td>&nbsp;</td>
	    			<td>
	    				<!-- Button Refresh -->
	    				<input id="btnRefresh" type="button" value="Refresh" onclick="location.href='';">
	    			</td>
	    			<td>&nbsp;</td>
	    			<td colspan="1" scope="col" >Select report:</td>
	    			<td  colspan="1" scope="col">
						<select  onchange="val()" id="id_select_report">
							<option value=""></option>
							<option value="1">Module Report</option>
							<option value="2">Transceiver Report</option>
		        		</select>
		    		</td>
	    			<td>
	    				<!-- Button Export -->
	    				<input id="btnExport1" type="button" value="Export">
	    			</td>
	    			<td>&nbsp;</td>
	    			<td>&nbsp;</td>
	    			<td>&nbsp;</td>
			    </tr>
    		</table> 
    		</div>
    		</div>
	<?php 
}
	function draw_dim_table(){
		?>
			<div id="id_master_header">
			</div>
			<div id="id_master_table">
			<table id ="id_table_dim" width="100%" border="0" align="center">
    	
  			<tr class="cls_dim_table_title" align="center">
  				<th width="1%"scope="col">No.</th>
  				<th width="3%" scope="col">Host_name</th>
  				<th width="3%" scope="col">Slot cases</th>
			    <th width="8%" scope="col" align="center">Module</th>
			    <th width="2%" scope="col" align="center">Port_empty </th>
			    <th width="8%" scope="col">Sub_module</th>
			    <th width="5%" scope="col">Hw</th>
			    <th width="5%" scope="col">Sw</th>
			    <th width="5%" scope="col">Fw</th> 
			    <th width="8%" scope="col">Serial</th> 
  			</tr>
  					<!-- do du lieu vao bang -->
   					<?php 
   					global $record ,$querydata;
   					//xac dinh bao nhiu dong
   					$sql_where  ="";
   					$sql_where1  ="";
   					if (isset($_GET['host_id'])) {
						if(!empty($_REQUEST['search'])){
							$sql_where.="";
						}else{
   						if($_GET['host_id']=='')
   						{
   							$sql_where.="and b.host_id ='0'";
   						}else{
   							$sql_where .="and b.host_id =".$_GET['host_id']." ";
   							
   						}}
   					}
   					if (isset($_GET['date'])) {
   						if($_GET['date']=='null')
   						{
   							$sql_where.=' and b.module_date =CURDATE()';
   						}else{
   							$sql_where .= ' and b.module_date ="'.$_GET['date'].'" ';
   						}
   					}
   					if (!empty($_REQUEST['search'])) {
   						$research=trim($_REQUEST['search']);
   						$sql_where1 .=" and ( c.sub_serial like '%".$research."%' or  a.module_name like '%".$research."%' or  a.module_serial like '%".$research."%' )";					
					}
   					if(($_REQUEST['run']=="dim")  and empty($_REQUEST['date']) and empty($_REQUEST['host_id']) and empty($_REQUEST['search']) )
   					{
   						$sql_where.="and b.host_id ='0' ";
   					}
   					$query = "SELECT count(a.module_serial)FROM plugin_nms_module a , plugin_nms_modulelog b, plugin_nms_submodule c
					where a.module_serial = b.module_serial and a.module_serial = c.module_serial  ".$sql_where."".$sql_where1."";
   					$record =db_fetch_cell($query);
				   	$user= ("SELECT a.module_name as module_name, a.hardware as module_hardware , a.firmware as module_firware , a.module_serial as module_serial ,
								 b.software as module_software ,b.module_num as module_num ,CONCAT_WS('',d.description,' (',d.hostname,')') as host_name 
								from plugin_nms_module a, plugin_nms_modulelog b ,plugin_nms_submodule c ,host d
								where a.module_serial= b.module_serial and a.module_serial = c.module_serial and b.host_id = d.id ".$sql_where."".$sql_where1."
					group by module_serial order by module_num limit 0, $record"); 
					if (empty($_GET['host_id'])) {
						$host_dim="";
					}
					else{
						$host_dim =  $_GET['host_id'];
					}
					if (empty($_GET['date'])) {
						$date="";
					}
					else{
						$date =  $_GET['date'];
					}
					if (empty($_GET['select'])) {
						$select="";
					}
					else{
						$select =  $_GET['select'];
					}
					if (empty($_GET['search'])) {
						$search="";
					}
					else{
						$search =  $_GET['search'];
					}
					?>
			<link rel="stylesheet" href="style/style.css">
			<script type="text/javascript" src="style/jquery.min.js"></script>
			<script type="text/javascript" src="style/jquery-ui.min.js"></script>
		    <script type="text/javascript">
		    	$("#id_host_dim").val('<?php echo $host_dim;?>');
		    	$("#id_select_report").val('<?php echo $select;?>');
		    	$("#id_search").val('<?php echo $search;?>');
		    </script>
    <?php 
				   	$result = db_fetch_assoc($user);
				   	$stt=1;
					foreach($result as $set) {
					$url = "?run=dim&date=".$_REQUEST['date']."&host_id=".$_REQUEST['host_id']."&search=".$search."";
					?>
					<tr id="id_dim_table"
					 	<?php if($stt%2==0) {echo 'class="xanhlot">';}
				 					else {echo 'class="xanhhong">';}?>>
						<td align="center" >&nbsp;<?php  echo $stt++?></td>
						<td align="center" >&nbsp;<?php  echo $set['host_name']?></td>
						<td align="center" >&nbsp;<?php  echo $set['module_num']?></td>
						<td align = "center" class="button1" ><a onclick = "openPopup(<?php echo$stt?>);"><?php echo  $set['module_name'] ?></a>
							  <!-- POPUP CONTENT -->
                                        <div id="popup<?php echo$stt?>" class="popup" style="width:800px; height: 500px; ">
                                                <div style="background-color: #00438C; height: 30px; color:white; ">
                                                <a href="#" class="close"/><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>
                                                <h2>Module's Detail Information</h2> </div>
                                                <div>
                                                   <div style="width: 100%;">
                                           <?php 
                                           		$host_id = get_host_id_from_tran($set['module_serial'],$_REQUEST['date']);
                                           ?>
									<table width="100%">
											  <tr align="left">
											    <td><h3>Host : <?php echo $host_id ?></h3></th>
											    <td>&nbsp;</th>
											    <td>&nbsp;</th>
											  </tr>
											  <tr align="left">
											    <td><h3>Slot cases : <?php echo $set['module_num']?></h3></td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											  </tr>
											  <tr align="left">
											    <td><h3>Module: <?php echo $set['module_name']?></h3></td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											  </tr>
											  <tr align="left">
											    <td><h3>Time: <?php echo $_REQUEST['date']?></h3></td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											    <td>&nbsp;</td>
											  </tr>
											</table>
											</div>
											
											<div style="height: 400px; width: 800px; overflow:scroll; "> 
											<table width="100%" >
											  <tr bgcolor="#6D88AD" height="20px" style="color: white;">
											  	<th scope="col">No:</th>
											    <th scope="col">Interface Name</th>
											    <th width="4%" scope="col">Rvalue</th>
												<th width="2%" scope="col">Thold</th>
											    <th scope="col">Status</th>
											    <th scope="col">Transceiver</th>
											    <th scope="col">Serial</th>
											  </tr>
											  <?php
											  $trans_data= db_fetch_assoc('SELECT  CONCAT("(",port_name,")","\t",port_alias) as interface_name,port_pause, port_status ,trans_name,rvalue,thold , trans_serial
																			from plugin_nms_translog
																			where host_id ='.$host_id.' and trans_date ="'.$_REQUEST['date'].'" and port_name like "__'.$set['module_num'].'/%"
																			Order by Convert(SUBSTRING(port_name,5,2),UNSIGNED INTEGER)');
											  $num=1;
											  foreach($trans_data as $item) { 
											  ?>
											<tr <?php if($item['port_pause']==1) {echo 'class="xanhlot"';}
				 							else {echo 'class="xanhhong"';} ?>>
											  	<td align="center"> <?php echo $num++?></td>
											    <td >&nbsp; <?php echo $item['interface_name']?></td>
											    <td align="center">&nbsp;<?php echo $item['rvalue'];?></td>
											    <td align="center">&nbsp;<?php 
											    		switch ($item['thold']){
															case '1': echo '++';
																		break;
															case '2': echo '+';
																		break;
															case '3': echo '-';
																		break;
															case '4': echo '--';
																		break;
															 
														}
											    ?></td>
											    <td align="center" >&nbsp; <?php echo $item['port_status']?></td>
											    <td >&nbsp; <?php echo $item['trans_name']?></td>
											    <td>&nbsp;<?php echo $item['trans_serial']?></td> 
							  				</tr>
											 <?php
											 } 
											 ?>
											</table>
											</div> 
											<div style="background-color: #E6E6FA; height: 50px" >
												<div>&nbsp;</div>
												<table>
												<?php
													 $port_used = get_sum_port_used($host_id, $_REQUEST['date'],$set['module_num']);
													 $port_empty= get_sum_port_empty($host_id, $_REQUEST['date'],$set['module_num']);
													 $port_notused = get_sum_port_notuse($host_id, $_REQUEST['date'],$set['module_num']);
													 $port_not_used= $port_empty + $port_notused;
												?>
												<tr>
												<td><h3>Sum of port_used :</h3></td>
												<td class="cls_alarm_color_up" align="center"> <?php echo $port_used ?></td>
												<td>&nbsp;</td>
												<td><h3>Sum of port_empty :</h3></td>
											    <td class="cls_alarm_color_down" align="center"> <?php echo $port_empty?></td>
												<td>&nbsp;</td>
												<td><h3>Sum of port_notuse :</h3></td>
												<td class="cls_alarm_color_pause" align="center"><?php echo $port_notused?></td>
												
												 </tr>
    										</table> 
											</div>   
                                                </div>
                                                        <!-- END POPUP CONTENT -->
                                        </div>
                         
                                        <div id="background"></div>
							
						</td>
					    <td align="center" >&nbsp;<?php echo $port_not_used ?> Port</td>
					    <td align="center" >&nbsp;<?php  ?></td>
					    <td align="center" >&nbsp;<?php echo $set['module_hardware']?></td>
					    <td align="center">&nbsp;<?php echo $set['module_software']?></td> 
					    <td align="center">&nbsp;<?php echo $set['module_firware']?></td> 
					    <td align="center">&nbsp;<?php  echo $set['module_serial'] ?></td>
					    	<?php 
					    		$db_sub = db_fetch_assoc('Select sub_name ,sub_serial,hardware from plugin_nms_submodule 
											where module_serial= "'.$set['module_serial'].'"');
								foreach ($db_sub as $setsub){
					    	?>
					  </tr>
					   <tr id="id_dim_table_1"
					    	<?php if($stt%2==0)
					    	 {echo 'class="xanhhong">';}
				 					else {echo 'class="xanhlot">';} ?>>
				 			<td align="center" >&nbsp;<?php  ?></td>
					    	<td align="center" >&nbsp;<?php  ?></td>
					    	<td align="center" >&nbsp;<?php  ?></td>
							<td align="center"><?php ?></td>
							<td align="center" >&nbsp;<?php  ?></td>
							<td align="center" >&nbsp;<?php echo $setsub['sub_name']?></td>
							<td align="center">&nbsp;<?php echo $setsub['hardware']?></td>
							<td align="center">&nbsp;<?php ?></td> 
							<td align="center">&nbsp;<?php ?></td> 
							<td align="center">&nbsp;<?php  echo $setsub['sub_serial'] ?></td>
						</tr>
					    	<?php 
					    	}
					    	?>
				<?php		   
				}
				?>
			</table>	
			</div>				
			<div id="id_master_header">
			</div>
			<div id="center_footer_up">
				  	 	 			<div class="countdown_time"  >
				  	 	 				<h3><div id="Timerefresh">Refresh time: </div></h3>
				  	 	 				<h3><div id ="countdown_time2"></div></h3>
				  	 	 			</div>
			</div>
		<?php 
		html_alarm_script_time();
	}
	?>